﻿using System;
using System.Collections.Generic;
using System.Text;

namespace DownUtil
{
    public class DownRequest
    {
        private string _Url;

        public string Url
        {
            get { return _Url; }
            set { _Url = value; }
        }

        private string _UserNAme;

        public string UserNAme
        {
            get { return _UserNAme; }
            set { _UserNAme = value; }
        }

        private string _Password;

        public string Password
        {
            get { return _Password; }
            set { _Password = value; }
        }

        public byte[] Serialize()
        {
            string Data = _Url + "|" + _UserNAme + "|" + _Password;
            return Encoding.UTF8.GetBytes(Convert.ToBase64String(Encoding.UTF8.GetBytes(Data)));
        }
        public void UnSerialize(byte[] Data)
        {
            string PropertyString = Encoding.UTF8.GetString(Convert.FromBase64String(Encoding.UTF8.GetString(Data)));
            string[] PropertyItem = PropertyString.Split('|');
            if (PropertyItem.Length < 3)
            {
                throw new SerializeWrongDataException(PropertyString);
            }
            else
            {
                _Url = PropertyItem[0];
                _UserNAme = PropertyItem[1];
                _Password = PropertyItem[2];
            }
        }

    }
    public class SerializeWrongDataException : Exception
    {
        public SerializeWrongDataException(string SerializedString)
            : base("反序列化失败：" + SerializedString)
        {
        }
    }
}
